// JavaScript Document
$(document).ready(function(e)
{
	var d=new Date();
	var y=d.getFullYear();
	$('#myModaledit #batal').click(function()
	{
		document.frm7.reset();
		$('#myModaledit').modal('hide')
	})
		tglNow('#myModaledit #tgl_trans');
		$('#myModaledit #tgl_trans').datepicker()//.dynDateTime({'range':[(y-1),y]});
		tglNow('#myModalAddNew #tgl_trans');
		$('#myModalAddNew #tgl_trans').datepicker();
		$('#myModalAddNew #tgl_trans').datepicker("option", "yearRange","-1:+0");//.dynDateTime({'range':[(y-1),y]})
		$('#myModalAddNew #tgl_trans').change(function(){get_no_jurnal();});
	//add new jurnal
	_get_tahun();
	$('#myModalAddNew #b_Simpan').click(function(){_simpan_jurnal();})
	$('#myModalAddNew #d_Simpan').click(function(){ $('#myModalAddNew').modal('hide');})
	//click add transaksi
	$('#myModaledit #add_t').click(function(){_add_content($('#myModaledit #id_jurnal').val());})
	//add new jurnal
	$('#p-table1').click(function(){tglNow('#myModalAddNew #tgl_trans');get_no_jurnal();$('#myModalAddNew').modal('show');})
	//simpan transaksi jurnal
	$('#myModalAddC #b_Tambah').click(function(){_simpan_jurnal_detail()})
	$('#myModalAddC #d_Tambah').click(function(){ $('#myModalAddC').modal('hide');})
		//_get_akun()
	$('#myModalAddC').typeahead({items:10})
	//acordion
	$('tr.tree-toggler').live('click',function(){
		$(this).parent().children('tr.'+$(this).attr('id')).toggle(300);
		$('#row_aktif').val($(this).attr('id'));
	})
	$('#bulan').live('change',function(){_show_jurnal('notrans');});
	$('#tahun').live('change',function(){_show_jurnal('notrans');});
	$('#id_unit')
		.val('1').select()
		.live('change',function(){_show_jurnal('notrans');});
})

/*===================================*/
function _simpan_jurnal()
{
	//simpan header jurnal
	$.post('set_jurnal_head',$('#frm1').serialize(),
	function(result)
	{
	$('#result').html(result).show().fadeOut(5000)
		_show_jurnal('notrans');	
		//_add_content($('#myModalAddNew #no_trans').val());
		$('#d_Simpan').click();
		$('#p-table2').click();
		$('#myModalAddNew').modal('hide');
	})
}
function _simpan_jurnal_detail()
{
	//simpan content jurnal
	$.post('set_jurnal_content',$('#frm3').serialize(),
	function(result)
	{
	$('#result').html(result).show().fadeOut(5000)
		_show_jurnal('notrans');	
		$('#myModalAddC').modal('hide');
	})
}
function _show_jurnal(fp)
{
	//show header jurnal list
	show_indicator('NewList',7);
	$.post('list_jurnal',$('#frm100').serialize(),
	function(result)
	{
		$('#NewList tbody').html(result);
		(fp!='')?
		fixHeader('NewList',50,300):'';
			var ra=$('#row_aktif').val();
			(ra!='')?$('.'+ra).show():'';

	})

}
function _add_jurnal_content(id) //depreciated 
{
	//show popup eidt jurnal atau add conten jurnal
	_get_detail_header(id);
	$('#myModaledit')
		.css({'width':(screen.availWidth-100),'left':'25%'})
		.modal('show');
}
function get_no_jurnal()
{
	//generat auto nomor jurnal
	$.post('get_nomor_jurnal',{'tgl':$('#myModalAddNew #tgl_trans').val()},
	function(result)
	{
		$('#myModalAddNew #id_jurnal').val(result).attr('readonly','readonly')
	})
}
function _get_detail_header(id)
{
	//header jurnal
	$.post('get_detail_header',{'id':id},
	function(result)
	{
		var r=$.parseJSON(result);
		$('#myModaledit #tgl_trans').val(tglFromSql(r.tgljurnal)).attr('readonly','readonly');
		$('#myModaledit #id_jurnal').val(r.notrans).attr('readonly','readonly');
		$('#myModaledit #keterangan').val(r.keterangan);
		$('#myModaledit #t_debet').val(r.debet).attr('readonly','readonly').priceFormat();
		$('#myModaledit #t_kredit').val(r.kredit).attr('readonly','readonly').priceFormat();	
	})
}
function _get_jurnal_content(id)
{
	$.post('get_detail_content',{'id':id},
	function(result)
	{
		var r=$.parseJSON(result)
		$('#myModalAddC #id_jurnal').val(r.notrans)
		$('#myModalAddC #tgl_trans').val(tglFromSql(r.tgljurnal));
		$('#myModalAddC #no_bukti').val(r.nbukti);
		$('#myModalAddC #bukti_lain').val(r.buktilain);
		$('#myModalAddC #id_akun').val(r.koderek+': '+r.cnama).select();
		$('#myModalAddC #tp_trans').val(r.reff).select();
		(r.debet=='0')?
			$('#myModalAddC #jml_trans').val(r.kredit):
			$('#myModalAddC #jml_trans').val(r.debet);
		formatAngka('myModalAddC #jml_trans')
		$('#myModalAddC #ket_trans').val(r.keterangan);
		$('#myModalAddC #cc_trans').val(r.costcenter).attr('readonly','readonly');
		$('#myModalAddC #id_trans').val(r.idtrans);
	})
}
function _add_content(id)
{
	//show popup add content jurnal
		_get_jurnal_content(id);		
		$('#myModalAddC').modal('show');
}
function _add_content_new(id)
{
	$('#myModalAddC #d_Tambah').click();
	tglNow('#myModalAddC #tgl_trans');
	$('#myModalAddC #id_jurnal').val(id).attr('readonly','readonly')
	formatAngka('myModalAddC #jml_trans')
	$('#myModalAddC').modal('show');
}
function btn_click(id,aksi)
{
	switch(aksi)
	{
		case 'edit':
			_add_content(id);
		break;
		case 'deleted':
			jConfirm('Yakin akan hapus data ini?','alert',function(r)
			{
				if(r)
				{
					$.post('del_jurnal_content',{'id':id},
					function(result)
					{
						_show_jurnal('notrans');
					})
				}
			})
		break;
		case 'add':
		var j=id.split(':')
			_add_content_new(j[0]);
		break;
		case 'edited':
		var j=id.split(':')
		_show_jurnal_head(j[0]);
			/*jPrompt('Update Keterangan jurnal',j[1],'Edit Jurnal',function(r){
				if(r)
				{
					$.post('upd_jurnal_header',{
						'id':j[0],
						'des':r
					},function(result){
						_show_jurnal('notrans');
					})
				}
			})*/
		break;
		case 'hapus':
		var j=id.split(':')
			jConfirm('Yakin jurnal ini akan dihapus?','alert',function(r)
			{
				if(r)
				{
					$.post('del_jurnal_head',{'id':j[0]},
					function(result)
					{
						_show_jurnal('notrans');
					})
						
				}
			})
		break;
	}
}
function _get_akun()
{
	$.post('get_akun',{'id':''},
	function(result){
		$('#myModalAddC #tphead').val(result)	
	})
}

function _get_tahun()
{
	$.post("get_tahun_jurnal",{'id':''},
	function(result){
		$('#tahun').html(result);
		_show_jurnal('notrans');
	})
}
function _show_jurnal_head(id)
{
	$.post('get_detail_head',{'id':id},
	function(result)
	{ 
		$('#myModalAddNew #frm1:reset').click();
		var r=$.parseJSON(result);
		$('#frm1 #id_unit').val(r.id_unit).select();
		$('#frm1 #tgl_trans').val(tglFromSql(r.tgljurnal));
		$('#frm1 #id_jurnal').val(r.notrans).attr('readonly','readonly');
		$('#frm1 #ket_trans').val(r.keterangan);
		$('#myModalAddNew').modal('show');
	})
}
function _simpan_update_head()
{
	$.post('set_jurnal_head',$('#frm1').serialize(),function(result){ _show_jurnal('notrans');})
}
